package com.qfedu.yanglao20admin.adhibition.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.common.annotation.LogInfo;
import com.qfedu.yanglao20.common.utils.OssUtils;
import com.qfedu.yanglao20.entity.adhibition.Adhibition;
import com.qfedu.yanglao20admin.adhibition.param.AdhibitionParam;
import com.qfedu.yanglao20admin.adhibition.service.AdhibitionService;
import com.qfedu.yanglao20admin.adhibition.vo.AdhibitionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * @author yujie
 * @data 2023/4/19 21:40
 */
@Api(tags = "应用服务管理")
@RestController
@RequestMapping("/apply")
public class AdhibitionController {

    @Resource
    private AdhibitionService adhibitionService;

    @LogInfo
    @ApiOperation(value = "分页展示应用服务信息", notes = "分页展示应用服务信息")
    @GetMapping("/page")
    public CommonResult<AdhibitionVO> page(AdhibitionParam param) {
        Page<AdhibitionVO> adhibitions = adhibitionService.pageAdhibition(param);
        return CommonResult.success(adhibitions);
    }


    @LogInfo
    @ApiOperation(value = "根据id删除应用服务信息", notes = "根据id删除应用服务信息")
    @GetMapping("/delete")
    public CommonResult<Adhibition> deleteAdhibition(@RequestParam @ApiParam("应用服务信息id") Integer id) {
        adhibitionService.deleteById(id);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "添加应用服务信息", notes = "添加应用服务信息")
    @PostMapping("/add")
    public CommonResult<Adhibition> addAdhibition(Adhibition adhibition) {
        adhibitionService.addAdhibition(adhibition);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "根据id查询应用服务信息", notes = "根据id查询应用服务信息")
    @GetMapping("/query")
    public CommonResult<Adhibition> queryAdhibition(@RequestParam @ApiParam("应用服务信息id") Integer id) {
        Adhibition adhibition = adhibitionService.queryAdhibition(id);
        return CommonResult.success(adhibition);
    }

    @LogInfo
    @ApiOperation(value = "修改应用服务信息", notes = "修改应用服务信息")
    @PostMapping("/update")
    public CommonResult<Adhibition> updateAdhibition(Adhibition adhibition) {
        adhibitionService.updateAdhibition(adhibition);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "上传图标", notes = "上传图标")
    @PostMapping("/upload")
    public CommonResult<Adhibition> upload(MultipartFile file) {

        String path = null;

        String name = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        name = uuid + name.substring(name.lastIndexOf("."));

        try {
            path = OssUtils.uploadFile2OSS(file.getInputStream(), name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.success(path);
    }
}
